package com.deuwise.job.customquartz;

import com.deuwise.common.utils.SpringContextHolder;
import com.deuwise.job.customquartz.base.JobRun;
import com.deuwise.job.customquartz.base.JobRunLogHelper;
import com.deuwise.job.entity.JobRunLogDo;
import com.deuwise.system.entity.BackUpDo;
import com.deuwise.system.service.BackUpDoService;
import com.deuwise.system.service.LogDoService;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import java.util.Date;


/**
 * Copyright
 *
 * @author chenwenchao
 * @date 2019/3/20
 * Description:
 */
public class JobBackupJobRunLog implements Job,JobRun {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public void run() {

        ApplicationContext applicationContext = SpringContextHolder.getApplicationContext();
        LogDoService logDoService=SpringContextHolder.getBean(LogDoService.class);
        BackUpDoService backUpDoService=SpringContextHolder.getBean(BackUpDoService.class);
        try {
            BackUpDo backUpDo = new BackUpDo();
            String backUrl = "";
            backUrl = logDoService.backUpLog(true);
            backUpDo.setBackUrl(backUrl);
            backUpDo.setSizeNum(0);
            backUpDo.setFileType(2);
            if (StringUtils.isNotBlank(backUrl)) {
                backUpDoService.setBackUpData(backUpDo,backUrl);
            }
            backUpDo.setCreateTime(new Date());
           boolean   isSuccess = backUpDoService.insert(backUpDo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("JobBackupJobRunLog备份任务运行日志：执行完毕=======================");
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //添加执行日志
        System.out.println("JobBackupJobRunLog备份任务运行日志：启动任务=======================");
        /*
         * 添加运行日志
         *
         * */
        JobRunLogHelper jobRunLogHelper= SpringContextHolder.getBean(JobRunLogHelper.class);
        JobRunLogDo jobRunLogDo=new JobRunLogDo();
        jobRunLogHelper.getBaseRunLog(context,jobRunLogDo, this.getClass());
    }
}
